Developer Documentation

QuickTime 4 API Documentation

3D Graphics Programming with QuickDraw 3D 1.5.4

Previous | QD3D Book | Overview | Chapter Contents | Next |

Controlling a Camera Position With a Pointing Device

By default, a 3D pointing device contributes to the position and orientation of the cursor. You can, however, reassign a particular pointing device so that it controls some other element in a user interface view, such as the position and orientation of the view's camera. To do this, you must first find the pointing device. Then you need to disconnect the device from the cursor and connect it to the desired user interface element.

Suppose that the pointing box you want to reassign is a knob box, which consists of a set of 12 knobs and associated alphanumeric displays. Six of the knobs control the standard position and orientation values, and the remaining 6 knobs are device-specific. Listing 4 shows first how to search for the knob box.

Listing 4 Searching for a particular 3D pointing device

TQ3ControllerRef        gBoxController              = NULL;
TQ3TrackerObject        gBoxTracker                 = NULL;
unsigned long           gBoxSerialNumber            = 0;

void MyFindKnobBox (void)
{
    TQ3ControllerRef        controller;
    char                    mySig[256];         /*controller signature*/
    char                    *boxSig =
                                "Knob Systems, Inc.::Knob Box Grandé";
    TQ3Boolean              isActive;

    /*Find the box controller.*/
    for (Q3Controller_Next(NULL, &controller); controller != NULL;
            Q3Controller_Next(controller, &controller)) {
        Q3Controller_GetSignature(controller, mySig, 256);
        Q3Controller_GetActivation(controller, &isActive);

        if (isActive && strncmp(mySig, boxSig, strlen(boxSig))
                                                                        == 0)
            gBoxController = controller;
    }

    /*If we found a knob box, remember it.*/
    if (gBoxController != NULL) {
        gBoxTracker = Q3Tracker_New(MyBoxNotifyFunc);
        if (gBoxTracker != NULL) {
            Q3Tracker_SetNotifyThresholds(gBoxTracker, 0.05, 0.05);
        }
        Q3Controller_SetTracker(gBoxController, gBoxTracker);
    }
}

Once you've found a knob box, you must connect it to the camera, but only for as long as your application's window is active. When the window is inactive, the box should revert to its previous function. Listing 5 defines two functions you should call when your application becomes active or inactive.

Listing 5 Activating and deactivating a pointing device

void MyOnActivation (void)
{
    /*Any knob box data goes to your tracker.*/
    if (gBoxController != NULL)
        Q3Controller_SetTracker(gBoxController, gBoxTracker);
}

void MyOnDeactivation (void)
{
    /*Any knob box data goes to the default tracker.*/
    if (gBoxController != NULL)
        Q3Controller_SetTracker(gBoxController, NULL);
}

As long as the knob box is attached to a view's camera, your application receives notification of changes in the knob box through the notify function MyBoxNotifyFunc , defined in Listing 6 . MyBoxNotifyFunc may be called at interrupt time. On Macintosh computers, you should wake up your process so that it can poll the tracker. This ensures that the application will recover control from the WaitNextEvent function.

Listing 6 Receiving notification of changes in a pointing device

TQ3Status MyBoxNotifyFunc (TQ3TrackerObject tracker,
                                    TQ3ControllerRef controller)
{
    MyOSWakeUpMyProcess();      /*wake up app; poll for data later*/
    return(kQ3Success);
}

The MyPollKnobBox function defined in Listing 7 shows how to poll for data from the device. Your application's idle procedure should call MyPollKnobBox .

Listing 7 Polling for data from a pointing device

void MyPollKnobBox (void)
{
    TQ3Boolean              changed;
    TQ3Point3D              position;
    TQ3Vector3D             delta;

    /*Get the current knob positions.*/
    changed = kQ3False;
    if (gBoxTracker != NULL) {
        Q3Tracker_GetPosition(gBoxTracker, &position, &delta,
                                        &changed, &gBoxSerialNumber);
    }

    /*Move camera and redraw if positions are new.*/
    if (changed) {
        MyComputeCameraFromKnobBox(&position, &orientation);
        MyRedrawScene();
    }
}

© 1997 Apple Computer, Inc.

Previous | QD3D Book | Overview | Chapter Contents | Next |